import Vue from 'vue'
import vueRouter from 'vue-router'
import Home from '@/components/Home'
import activeRule from '@/components/activeRule'

Vue.use(vueRouter)
let router=new vueRouter({
    mode: 'history',
    routes:[
        {
            path: '/home',
            name: 'Home',
            component: Home,
            meta: {
                index: 1,
            }
        },
        {
            path: '/activeRule',
            name: 'activeRule',
            component: activeRule,
            meta: {
                index: 2,
            }
        },
        {
            path: '*',
            redirect: '/home'
        }
    ]
})
// 全局路由权限校验-access_token
router.beforeEach((to, from, next) => {
    if (to.matched.some(record => record.meta.requireAuth)) {
        let access_token = localStorage.getItem("access_token")
        if (access_token && access_token !== '') {
            var instance = Vue.prototype.axios.create({});
            instance.defaults.headers.common["Authorization"] = access_token.toString();
            next();
        }
        else {
            if (!from.matched[0] || !from.matched[0].instances) {
                next();
            } else {
                const instances = from.matched[0].instances.default;
                instances.$p_message('酒有：请登录', 'error');
                setTimeout(() => {
                    next({
                        path: '/signIn'
                    })
                }, 800)
            }
        }
        if (to.meta.title) {
            document.title = to.meta.title
        } else {
            document.title = '酒有'
        }
    } else {
        if (to.meta.title) {
            document.title = to.meta.title
        }else{
            document.title = '酒有'
        }
        next();
    }
    
});
export default router
